﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace event_clock
{
    //声明一个委托类型，定义事件处理函数的格式
    public delegate void ClockHandler(object clock, ClockEventArgs args);

    //定义该事件参数
    public class ClockEventArgs
    {
        public int clock_h { get; set; }
        public int clock_m { get; set; }
        public int clock_s { get; set; }
    }

    //定义闹钟事件
    public class Clock_E
    {
        public event ClockHandler clock_e;
        public void clock_time (int h, int m, int s)
        {
            //显示现在时刻的时间
            Console.Write($"Now is at [{h}:{m}:{s}]");
            ClockEventArgs clock_args = new ClockEventArgs()
            {
                clock_h = h,clock_m = m,clock_s = s
            };
            //触发时钟事件
            clock_e (this, clock_args);
        }
    }
    public class Clock
    {
        //时钟时分秒和闹钟的时分秒
        public int h, m, s, a_h, a_m, a_s;
        //闹钟事件
        public Clock_E alarm = new Clock_E();

        //设定开始时间
        public void setstarttime(int h1,int m1,int s1)
        {
            h = h1;m = m1;s = s1;
        }
        //设定闹钟时间
        public void setalarmtime(int h, int m, int s)
        {
            a_h = h; a_m = m; a_s = s;
        }
        //时间流逝函数
        public void timerun()
        {
            this.s++;
            if (s == 60)
            {
                m++; s = 0;
                if (m == 60)
                {
                    h++; m = 0;
                    if (h == 24)
                        h = 0;
                }
            }
        }
        public Clock()
        {
            alarm.clock_e += tick;
            alarm.clock_e += new ClockHandler(alarm1);
        }
        void alarm1(object sender, ClockEventArgs args)
        {
            if (args.clock_h == this.a_h && args.clock_m == this.a_m && args.clock_s == this.a_s)
                for (int i = 0; i < 5; i++)
                {
                    timerun();
                    Console.WriteLine("Di! Di! Di!!!");
                    System.Threading.Thread.Sleep(1000);
                }
        }
        void tick(object sender, ClockEventArgs args)
        {
            timerun();
            Console.WriteLine("Tick~");
        }
        //闹钟运行函数
        public void run()
        {
            while (true)
            {
                this.alarm.clock_time(this.h, this.m, this.s);
                System.Threading.Thread.Sleep(1000);
            }
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Clock clock1 = new Clock();
            clock1.setstarttime(23, 59, 55);
            clock1.setalarmtime(0, 0, 3);
            clock1.run();
        }
    }
}
